<?php

class Mithos_Connection {

    protected static $_connection = null;

    public static function connect($config = null) {
        if (empty($config)) {
            throw new Mithos_Connection_Exception('Invalid parameter for making connection');
        }

        if (!self::enabled()) {
            throw new Mithos_Connection_Exception('MSSQL extenssion is not enabled');
        }

        self::$_connection = mssql_connect($config['host'], $config['username'], $config['password'], true);
        if (!mssql_select_db($config['dbname'], self::$_connection)) {
            throw new Mithos_Connection_Exception('Unable to connect to the database');
        }
    }

    public static function connected() {
        return is_resource(self::$_connection);
    }

    public static function disconnect() {
        if (mssql_close(self::getConnection())) {
            self::$_connection = null;
        }
    }

    public static function enabled() {
        return extension_loaded('mssql');
    }

    public static function getConnection() {
        return self::$_connection;
    }

}